#ifndef DATABASEDIFF_H
#define DATABASEDIFF_H

#include <QObject>
#include <QSqlDatabase>
#include <QString>
#include <QMap>
#include <QList>

// 表示表结构的信息
struct TableInfo {
    QString name;
    QMap<QString, QString> columns; // 列名 -> 数据类型
    QStringList primaryKey;
    QMap<QString, QString> foreignKeys; // 列名 -> 引用表(列)
    
    TableInfo() = default;
    TableInfo(const QString &n) : name(n) {}
};

// 表示数据库结构的信息
struct DatabaseInfo {
    QString name;
    QList<TableInfo> tables;
    
    DatabaseInfo() = default;
    DatabaseInfo(const QString &n) : name(n) {}
};

// 表示两个表之间的差异
struct TableDifference {
    enum Type {
        Added,
        Removed,
        Modified
    };
    
    Type type;
    QString tableName;
    QString details;
    
    TableDifference(Type t, const QString &name, const QString &desc = QString())
        : type(t), tableName(name), details(desc) {}
};

// 表示两个数据库之间的差异
struct DatabaseDifference {
    QString databaseName1;
    QString databaseName2;
    QList<TableDifference> tableDifferences;
    QString summary;
};

class DatabaseDiff : public QObject
{
    Q_OBJECT

public:
    explicit DatabaseDiff(QObject *parent = nullptr);
    
    DatabaseInfo extractDatabaseInfo(const QSqlDatabase &db);
    DatabaseDifference compareDatabases(const QSqlDatabase &db1, const QSqlDatabase &db2);
    
signals:
    void comparisonProgress(int percentage);
    void comparisonFinished(const DatabaseDifference &diff);
    void comparisonError(const QString &error);

private:
    TableInfo extractTableInfo(const QSqlDatabase &db, const QString &tableName);
    QList<TableDifference> compareTables(const TableInfo &table1, const TableInfo &table2);
    QString getColumnType(const QSqlDatabase &db, const QString &tableName, const QString &columnName);
    
    QString generateSummary(const QList<TableDifference> &differences);
};

#endif // DATABASEDIFF_H